Can't get [DateTime]::TryParseExact to work using PowerShell
I've used TryParseExact before in C# but I can't get it to work in PowerShell. I am a PowerShell neophyte...here is my example:
[System.Globalization.CultureInfo]$provider = [System.Globalization.CultureInfo]::InvariantCulture
$dateString = "Sun 15 Jun 2008 8:30 AM -06:00"
$format = "ddd dd MMM yyyy h:mm tt zzz"
$parsedDate = get-date
$result = [DateTime]::TryParseExact($dateString, $format, $provider, [System.Globalization.DateTimeStyles]::None, [ref]$parsedDate)
# $result = [DateTime]::ParseExact($dateString, $format, $provider)
Write-Host "`$result is $result"
The call to ParseExact works fine (if I uncomment it and comment out the call to TryParseExact).
What am I missing?
Thanks,
Ray
January 14th, 2010 4:22pm
Are you trying this with PowerShell v1 or v2?
v2 complains:
Cannot find an overload for "TryParseExact" and the argument count: "5".
At line:1 char:36
+ $result = [DateTime]::TryParseExact <<<< ($dateString,$format, $provider,"None",[ref]$parsedDate)
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
Unfortunately, I think there are issues with v2's handling of constructor-type errors.
There's only 2 supported constructors for this method.
-
Edited by
Marco ShawModerator
Thursday, January 14, 2010 4:47 PM
typo
January 14th, 2010 4:47pm
Are you trying this with PowerShell v1 or v2?
v2 complains:
Cannot find an overload for "TryParseExact" and the argument count: "5".
At line:1 char:36
+ $result = [DateTime]::TryParseExact <<<< ($dateString,$format, $provider,"None",[ref]$parsedDate)
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
Unfortunately, I think there are issues with v2's handling of constructor-type errors.
There's only 2 supported constructors for this method.
-
Edited by
Marco ShawModerator
Thursday, January 14, 2010 4:47 PM
typo
January 14th, 2010 4:47pm
I can't get it to work, either.
Woud using try/catch with parseexact be a possible workaround?
January 14th, 2010 4:53pm
I am using V2 under Win7 Ultimate. And your error is the same as mine. I've tried to be as explicit as possible with the input (i.e. supply real types vs. $null).
January 14th, 2010 4:59pm
Karl,
Yep. That's where I got the ParseExact example. I couldn't find one anywhere using TryParseExact.
Ray
January 14th, 2010 7:27pm
mjolinor,
I will play around with using try/catch around a call to ParseExact. That sounds like it will allow me to accomplish the same thing.
Thanks,
Ray
January 14th, 2010 7:28pm
Wow - I should learn to read - I though the example was TryParseExact - sorry about that.
Can you post the C# code you use?
Karl
January 14th, 2010 9:43pm
You need to declase the [ref] not in the MethodCall but in the declaration of the variable like this :
[System.Globalization.CultureInfo]$provider = [System.Globalization.CultureInfo]::InvariantCulture
$dateString = "Sun 15 Jun 2008 8:30 AM -06:00"
$format = "ddd dd MMM yyyy h:mm tt zzz"
[ref]$parsedDate = get-date
[DateTime]::TryParseExact($dateString, $format,$provider,[System.Globalization.DateTimeStyles]::None,$parseddate)
$parseddate
Greetings MOW
-
Proposed as answer by
Just KarlMicrosoft community contributor
Friday, January 15, 2010 2:58 PM
-
Marked as answer by
raymegal2
Friday, January 15, 2010 4:27 PM
January 15th, 2010 10:55am
You need to declase the [ref] not in the MethodCall but in the declaration of the variable like this :
[System.Globalization.CultureInfo]$provider = [System.Globalization.CultureInfo]::InvariantCulture
$dateString = "Sun 15 Jun 2008 8:30 AM -06:00"
$format = "ddd dd MMM yyyy h:mm tt zzz"
[ref]$parsedDate = get-date
[DateTime]::TryParseExact($dateString, $format,$provider,[System.Globalization.DateTimeStyles]::None,$parseddate)
$parseddate
Greetings MOW
-
Proposed as answer by
Just KarlMicrosoft community contributor
Friday, January 15, 2010 2:58 PM
-
Marked as answer by
raymegal2
Friday, January 15, 2010 4:27 PM
January 15th, 2010 10:55am
MOW,
Very nice. That did it. Thanks!
Ray
January 15th, 2010 4:28pm
Found this thread on my way to the solution I needed below.
What MOW has is definitely correct, but it makes the object type of $parseddate a powershell reference [System.Management.Automation.PSReference]. By making the small changes below, $parseddate will be of System.DateTime and allow all of the .NET
DateTime methods/properties to be used.
[System.Globalization.CultureInfo]$provider = [System.Globalization.CultureInfo]::InvariantCulture
$dateString = "Sun 15 Jun 2008 8:30 AM -06:00"
$format = "ddd dd MMM yyyy h:mm tt zzz"
[System.DateTime]$parsedDate = get-date
[DateTime]::TryParseExact($dateString, $format,$provider,[System.Globalization.DateTimeStyles]::None,[ref]$parseddate)
$parseddate
July 29th, 2015 12:44am
How to use date parser in PowerShell:
$parsedDate=0 # create a variable to load into
if([DateTime]::TryParseExact('Sun 15 Jun 2008 8:30 AM -06:00', 'ddd dd MMM yyyy h:mm tt zzz',$null,'None',[ref]$parseddate)){
$parseddate
}else{
Write-Host 'Date string cannot be parsed'
}
This is convenient because it does not throw an exception.
In most cases this will work as required.
[DateTime]::Parse('Sun 15 Jun 2008 8:30 AM -06:00')
Try it.
This thread is very old and does not really apply well to Net Framework 2 and later.
July 29th, 2015 6:39am